package controllers;

import forms.AutoSiteForm;
import forms.BlogForm;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.AutoSiteData;
import data.BlogData;
import data.UserData;
import debugs.AutoSiteDebug;
import debugs.BlogDebug;

import models.AutoSite;
import models.Blog;
import models.User;

public class EditBlogServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String gmail = request.getUserPrincipal().getName();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String blogId = URLDecoder.decode(path.toLowerCase().replaceAll("/edit-blog/", ""),"UTF-8");
		BlogData blogData = new BlogData();
		Blog blog = blogData.getById(blogId);
		if (blog == null) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		TreeMap check = new TreeMap();
		
		//Get blogs by gmail
		ArrayList<Blog> blogs = blogData.getAllByGmail(gmail);
		AutoSiteData aData = new AutoSiteData();
		ArrayList<AutoSite> sites = aData.getByBlogId(blogId);
		
		request.setAttribute("blog", blog);
		request.setAttribute("blogs", blogs);
		request.setAttribute("sites", sites);
		request.setAttribute("check", check);
		request.setAttribute("form", "edit");
		try {
            request.getRequestDispatcher("/pages/form_blog.jsp").forward(request, response);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String gmail = request.getUserPrincipal().getName();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String blogId = URLDecoder.decode(path.toLowerCase().replaceAll("/edit-blog/", ""),"UTF-8");
		
		BlogData bData =  new BlogData();
		Blog blog = new Blog();
		
		//Get value form and check valid
		TreeMap form = BlogForm.checkValid(request, blog);
		blog =(Blog) form.get("blog");
		TreeMap check = (TreeMap) form.get("checkValid");
		
		//Save data
		if (check.size() == 0){
			blog.setGmail(gmail);
			bData.insert(blog);
			BlogDebug.debug(blog);
			request.setAttribute("success", "Update success blog " + blog.getBlogId());
		}
		
		//Get blogs by gmail
		ArrayList<Blog> blogs = bData.getAllByGmail(gmail);
		AutoSiteData aData = new AutoSiteData();
		ArrayList<AutoSite> sites = aData.getByBlogId(blogId);

		request.setAttribute("blog", blog);
		request.setAttribute("blogs", blogs);
		request.setAttribute("sites", sites);
		request.setAttribute("check", check);
		request.setAttribute("form", "edit");
		try {
            request.getRequestDispatcher("/pages/form_blog.jsp").forward(request, response);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
}
